package statement;

public class BreakOuterLoop {
    public static void main(String[] args) {
        // 使用标签结束外部循环
        outerLoop: // 定义外部循环标签
        for (int i = 1; i <= 5; i++) {
            System.out.println("外层循环: " + i);

            for (int j = 1; j <= 5; j++) {
                System.out.println("  内层循环: " + j);

                // 当满足特定条件时，结束外部循环
                if (i == 3 && j == 2) {
                    System.out.println("  满足条件，结束所有循环");
                    break outerLoop; // 终止外部循环
                }
            }
        }

        System.out.println("\n循环结束后的代码");

        // 实际应用示例：查找二维数组中的元素
        int[][] matrix = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        int target = 5;
        boolean found = false;

        search:
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == target) {
                    System.out.println("\n找到目标元素 " + target + "，位置: (" + i + "," + j + ")");
                    found = true;
                    break search;
                }
            }
        }

        if (!found) {
            System.out.println("\n未找到目标元素 " + target);
        }
    }
}
